if (window.top !== window.self) {
    window.top.location = window.location
}
//一般直接写在一个js文件中
layui.config({
    base: '/script/'
});
// layui.use(['myStorage']找这个文件static/script/myStorage.js
layui.use(['myStorage', 'layer'], function () {
    var $ = layui.jquery,
        layer = layui.layer, //加载layer模块
        storage = layui.myStorage;
    $(document).on('click', '.ajax-login', function (e) {
        e.preventDefault();
        var form = $(this).parents("form");
        var url = form.attr("action");
        var serializeArray = form.serializeArray();

        $.post(url, serializeArray, function (result, textStatus, request) {
            if (result.code !== 200) {
                $('.captcha-img').click();
            } else if (result.code === 200) {
                let token = request.getResponseHeader("Authorization");
                var username = result.data.username,
                url = result.data.login_url.url;
                //这里将获取到的token存储起来，下次用的时候就使用这个tokenId
                storage.setItem({
                    name: "token",
                    value: token,
                    expires: 15 * 60 * 1000
                })
                storage.setItem({
                    name: "username",
                    value: username,
                    expires: 15 * 60 * 1000
                })
                // 登入成功的提示与跳转
                layer.msg(result.msg, {offset: '15px', time: 3000, icon: 1}
                    , function () {
                        window.location.href = url;
                    });

            } else {
                layer.msg(result.msg, {
                    offset: '15px',
                    icon: 2,
                    time: 3000
                });
            }
        });
    });
    $('.layui-layer-loading').hide();
});